/**
 * Yi-Jing (I Ching) is one of the most ancient literature in Chinese history.  A more detailed
 * description of the system can be found at <a href="{@link}http://en.wikipedia.org/wiki/I_Ching>
 * Wikipedia: I Ching</a>.
 */
package divination.yijing;

import java.util.Random;

/**
 * @author Ting-Shuo Yo
 *
 */
public class DivinationCoins {
	public Hexagram original;
	public Hexagram varied;
	public DivinationCoins(Random rd){
    	// 
    	Yao[] gua1 = new Yao[6];
    	Yao[] gua2 = new Yao[6];
    	// Divination for 6 Yao-s with change-flag
    	for (int i=0;i<6;i++){
        	Boolean[] siXian = throwCoins(rd);
    		gua1[i] = new Yao(siXian[0]);							// The original Yao
    		gua2[i] = new Yao(siXian[1]?(!siXian[0]):(siXian[0]));	// The varied Yao
    	}
    	original = new Hexagram(gua1);
    	varied = new Hexagram(gua2);
	}
	
    /**
     * Throw 3 coins with a given random seed, and return:
 	 *   3: old-Yang: all 3 are Yang (true)
	 *   2: young-Yang: 2 of 3 are Yang
	 *   1: young-Yin: 2 of 3 are Yin
	 *   0: old-Yin:  all 3 are Yin (false)
     * @param rd Random, a random seed to renew the Yao
     */
    public Boolean[] throwCoins(Random rd){
    	/* Throw 3 coins with the given random seed */
    	Boolean d1 = rd.nextBoolean();
    	Boolean d2 = rd.nextBoolean();
    	Boolean d3 = rd.nextBoolean();
    	//System.out.println(d1.toString()+d2.toString()+d3.toString());
    	// The result
    	Boolean[] results = new Boolean[2];
		// Yin or Yang (0 or 2)
		results[0] = (((((d1)?1:0) + ((d2)?1:0) + ((d3)?1:0)))>1);
		// Variable or unchanged (all three tests are the same, the it is variable)
		results[1] = ((d1==d2)&&(d1==d3));
		return(results);
    }
}
